<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="经销商编码" prop="BPCNUM">
        <el-input
          v-model="queryParams.BPCNUM"
          placeholder="请输入经销商编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="BPCNAM">
        <el-input
          v-model="queryParams.BPCNAM"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['x3base:gesybpc:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['x3base:gesybpc:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['x3base:gesybpc:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['x3base:gesybpc:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="gesybpcList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="BPCNAM" />
      <el-table-column label="经销商编码" align="center" prop="BPCNUM" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['x3base:gesybpc:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['x3base:gesybpc:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改经销商对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form style="display: flex; flex-wrap: wrap">
          <el-form-item label="公司名称" prop="BPCNAM">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.BPCNAM"
              placeholder="请输入公司名称"
            />
          </el-form-item>
          <el-form-item label="经销商编码" prop="BPCNUM">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.BPCNUM"
              placeholder="请输入经销商编码"
            />
          </el-form-item>
          <el-form-item label="地点" prop="YFCY">
            <el-input
              style="width: 300px"
              v-model="form.YFCY"
              placeholder="请输入地点"
            />
          </el-form-item>
          <el-form-item label="订货权限" prop="YDHQX">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YDHQX"
              placeholder="请选择订货权限"
            />
          </el-form-item>
          <el-form-item label="订货权限" prop="YSFZK">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSFZK"
            />
          </el-form-item>
          <el-form-item label="营业执照" prop="YZZ">
            <el-input
              style="width: 300px"
              v-model="form.YZZ"
              placeholder="请输入营业执照"
            />
          </el-form-item>
          <el-form-item label="是否三证合一" prop="YSZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSZ"
            />
          </el-form-item>
          <el-form-item label="纳税人性质" prop="YNSR">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YNSR"
            />
          </el-form-item>
          <el-form-item label="食品经营许可证起始日期" prop="YSPRQS">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSPRQS"
              placeholder="请输入食品经营许可证起始日期"
            />
          </el-form-item>
          <el-form-item label="食品经营许可证终止日期" prop="YSPRQE">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSPRQE"
              placeholder="请输入食品经营许可证终止日期"
            />
          </el-form-item>
          <el-form-item label="税号" prop="YSH">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSH"
              placeholder="请输入税号"
            />
          </el-form-item>
          <el-form-item label="法人" prop="YFR">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFR"
              placeholder="请输入法人"
            />
          </el-form-item>
          <el-form-item label="法人电话" prop="YFRP">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFRP"
              placeholder="请输入法人电话"
            />
          </el-form-item>
          <el-form-item label="法人身份证号码" prop="YFRSFZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFRSFZ"
              placeholder="请输入法人身份证号码"
            />
          </el-form-item>
          <el-form-item label="开户银行(对公账户)" prop="YKHYH">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YKHYH"
              placeholder="请输入开户银行(对公账户)"
            />
          </el-form-item>
          <el-form-item label="银行账户(对公账户)" prop="YYHZH">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YYHZH"
              placeholder="请输入银行账户(对公账户)"
            />
          </el-form-item>
          <el-form-item label="邮编" prop="YYB">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YYB"
              placeholder="请输入邮编"
            />
          </el-form-item>
          <el-form-item label="E-mail" prop="YYX">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YYX"
              placeholder="请输入E-mail"
            />
          </el-form-item>
          <el-form-item label="注册地址" prop="YZCDZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YZCDZ"
              placeholder="请输入注册地址"
            />
          </el-form-item>
          <el-form-item label="收货联系人" prop="YSHR">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSHR"
              placeholder="请输入收货联系人"
            />
          </el-form-item>
          <el-form-item label="收货联系电话" prop="YSHDH">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSHDH"
              placeholder="请输入收货联系电话"
            />
          </el-form-item>
          <el-form-item label="收货地址" prop="YSHDZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YSHDZ"
              placeholder="请输入收货地址"
            />
          </el-form-item>
          <el-form-item label="发票地址(税票上)" prop="YFPDZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFPDZ"
              placeholder="请输入发票地址(税票上)"
            />
          </el-form-item>
          <el-form-item label="发票资料邮寄地址" prop="YFPYJDZ">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFPYJDZ"
              placeholder="请输入发票资料邮寄地址"
            />
          </el-form-item>
          <el-form-item label="发票邮寄联系人" prop="YFPYJR">
            <el-input
              style="width: 300px; margin-right: 50px"
              v-model="form.YFPYJR"
              placeholder="请输入发票邮寄联系人"
            />
          </el-form-item>
        </el-form>
        <el-tabs v-model="activeName">
          <el-tab-pane label="经销商维护" name="tab1"> </el-tab-pane>
          <el-tab-pane label="经销商产品及其任务目标" name="tab2">
          </el-tab-pane>
          <el-tab-pane label="推广专员维护" name="tab3"
            >推广专员维护的内容</el-tab-pane
          >
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGesybpc,
  getGesybpc,
  delGesybpc,
  addGesybpc,
  updateGesybpc,
} from "@/api/x3base/gesybpc";
export default {
  components: {},
  name: "Gesybpc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 经销商表格数据
      gesybpcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        BPCNUM: null,
        BPCNAM: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isAdd: false,
      activeName: "tab2",
      detailArray: [
        { label: "公司全称", prop: "BPCNAM", placeholder: "请输入公司全称" },
        {
          label: "经销商编码",
          prop: "BPCNUM",
          placeholder: "请输入经销商编码",
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询经销商列表 */
    getList() {
      this.loading = true;
      listGesybpc(this.queryParams).then((response) => {
        this.gesybpcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        BPCNUM: null,
        BPCNAM: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.BPCNUM);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加经销商";
      this.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const BPCNUM = row.BPCNUM || this.ids;
      this.isAdd = false;
      getGesybpc(BPCNUM).then((response) => {
        console.log("获取类型", response.data);
        this.form = response.data["HEAD"];
        this.form.detail = response.data["HEAD1"];
        this.open = true;
        this.title = "修改经销商";
        console.log("明细表", this.form.detail);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log("看看提交的数据", this.form);
        if (valid) {
          if (this.isAdd != true) {
            updateGesybpc(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGesybpc(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const BPCNUMs = row.BPCNUM || this.ids;
      this.$modal
        .confirm('是否确认删除经销商编号为"' + BPCNUMs + '"的数据项？')
        .then(function () {
          return delGesybpc(BPCNUMs);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "x3base/gesybpc/export",
        {
          ...this.queryParams,
        },
        `gesybpc_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
